<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String invoice_type = request.getParameter("invoice_type") == null ? "0" : request.getParameter("invoice_type");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	String invoice_title = request.getParameter("invoice_title") == null ? "" : request.getParameter("invoice_title");
	String invoice_id = request.getParameter("invoiceId") == null ? "" : request.getParameter("invoiceId");
	String account_receivable=request.getParameter("account_receivable") == null ? "0" : request.getParameter("account_receivable");
	String has_receivable=request.getParameter("has_receivable") == null ? "0" : request.getParameter("has_receivable");
	String warranty_value=request.getParameter("warranty_value") == null ? "0" : request.getParameter("warranty_value");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建项目收款":"编辑项目收款" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		$(function(){
			$('#in_money').numberbox('setValue',parseFloat('<%=account_receivable%>')-parseFloat('<%=has_receivable%>')-parseFloat('<%=warranty_value%>'));
			$('#warranty_value').numberbox('setValue','<%=warranty_value%>');
			if('<%=flag%>' == 1){
				$('#ff').form('load','<%=contextPath %>/springMVC/xmFinance/getInMoneyById?id=<%=id%>');
			} 
		});
		
		function addXMInvoiceRequest(obj){
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=encodeURIComponent(jQuery("#xm_name").val());	
			var xm_code=jQuery("#xm_code").val();
			var invoice_id=jQuery("#invoice_id").val();
			var remark=encodeURIComponent(jQuery('#remark').val());
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			if(!checkNullOREmpty(invoice_id,'缺少票据')){return;}
			var in_money=jQuery("#in_money").numberbox('getValue');
			var manage_money=jQuery("#manage_money").numberbox('getValue');
		    if(parseFloat(in_money) < parseFloat(manage_money)){
		    	$.messager.alert('提示','收款金额不能小于管理费！');
				return ;
		    }
		    var account_receivable=jQuery("#account_receivable").val();
		    var has_receivable=jQuery("#has_receivable").val();
		    var checkMoney=parseFloat(account_receivable)-parseFloat(has_receivable)-parseFloat(warranty_value)-parseFloat(in_money);
		     if(parseFloat(checkMoney)<0){
		    	$.messager.alert('提示','收款金额不能大于能收款的最大金额！');
				return ;
		    }
		    
			var param = "remark="+remark
			+"&xm_invoice_id="+invoice_id+"&in_money="+in_money+"&manage_money="+manage_money
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name;
			
			$(obj).hide();
			$.ajax({
				type:'POST',
				<%=isAdd?"url:'"+contextPath+"/springMVC/xmFinance/addXMEndInMoney',":"url:'"+contextPath+"/springMVC/xmFinance/updateXMEndInMoney?id="+id+"',"%>
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						if('<%=callback%>' == '1'){
							openDialogResize("<%=contextPath %>/springViews/xm/xmInMoney.jsp?invoice_id=<%=invoice_id%>",1000, 800);
						}else{
							eval('window.opener.<%=callback%>();');
						}
						window.close();
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>">
						<input type="hidden" id="invoice_id" name="invoice_id" readOnly value="<%=invoice_id%>">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>">
					</td>
				</tr>

				<tr>	
					<td align="right" class="td1" nowrap>经办人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
					<td align="right" class="td1" nowrap>发票抬头：</td>
					<td class="td2">
						<input  id="invoice_title" name="invoice_title" readOnly value="<%=invoice_title%>">
					</td>
				</tr>
				<tr>	
					<td align="right" class="td1" nowrap>开票金额：</td>
					<td class="td2" nowrap>
						<input  id="account_receivable" name="account_receivable" readOnly value="<%=account_receivable%>">
					</td>
					<td align="right" class="td1" nowrap>已收款金额：</td>
					<td class="td2">
						<input  id="has_receivable" name="has_receivable" readOnly value="<%=has_receivable%>">
					</td>
				</tr>
				<tr>
			        <td align="right" class="td1" nowrap>质保金：</td>
					<td class="td2" >
					<input  id="warranty_value" name="warranty_value"  class="easyui-numberbox" data-options="min:0,precision:2,disabled:true">
					</td>
				</tr>
				<tr>
			        <td align="right" class="td1" nowrap>管理公司收款：</td>
					<td class="td2" >
					<input  id="in_money" name="in_money"  class="easyui-numberbox" data-options="min:0,precision:2" value="0">
					</td>
					<td align="right" class="td1" nowrap>管理费用：</td>
					<td class="td2"  >
						<input  id="manage_money" name="manage_money"  class="easyui-numberbox" data-options="min:0,precision:2" value="0">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
						   <%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMInvoiceRequest(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
							<%} %>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>